/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.caracol.db.AccesoDatos;
import com.caracol.db.CaracolQueryGenerator;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Roque Rueda
 */
public class Hijos {
    
    private static CaracolQueryGenerator caracolQuery = new CaracolQueryGenerator();
    
    private int id;
    private int idOficial;
    private String nombre;
    private String apellidoPaterno;
    private String apellidoMaterno;
    
    private static final String TABLE = "hijos";
    private final static String ID="id";
    private final static String ID_OFICIAL = "id_oficial";
    private final static String NOMBRE = "nombre";
    private final static String APELLIDO_PATERNO = "apellido_paterno";
    private final static String APELLIDO_MATERNO = "apellido_materno";

    private Hijos(int id, int idOficial, String nombre, String apellidoPaterno, String apellidoMaterno) {
        this.id = id;
        this.idOficial = idOficial;
        this.nombre = nombre;
        this.apellidoPaterno = apellidoPaterno;
        this.apellidoMaterno = apellidoMaterno;
    }

    public Hijos() {
        this(0, 0 , "", "", "");
    }
    
    public Hijos(int idOficial, String nombre, String apellidoPaterno, String apellidoMaterno)
    {
        this(0, idOficial, nombre, apellidoPaterno, apellidoMaterno);
    }
    
     /**
     * Get a single cargo store in the database by searching it's id
     * in the table
     * @param id identifier that is going to be search
     * @return cargo instance initialize with the value of the database
     * if it's found the given id otherwise returns null
     * @throws SQLException if some db exception happend during this operation
     * this is throw again
     * @throws Exception if some exception happend during the execution of this
     * operation this is throw
     */
    public static Hijos getEscolaridad(int id) throws SQLException, Exception {
        Hijos hijo = null;
        String[] columns = new String[] { ID, ID_OFICIAL, NOMBRE, APELLIDO_PATERNO, 
            APELLIDO_MATERNO };
        
        Map<String, String> where = new LinkedHashMap<String, String>();
        where.put(ID, new Integer(id).toString());
        
        List<Object> rawCargo = caracolQuery.query(columns, TABLE, where, null, null, null, null);
        
        if (rawCargo != null && rawCargo.size() > 0) {
            List<Object> row = (List<Object>) rawCargo.get(0);
            hijo  = new Hijos();
            hijo.id = id;
            hijo.setIdOficial(Integer.parseInt(row.get(1).toString()));
            hijo.setNombre(row.get(2).toString());
            hijo.setApellidoPaterno(row.get(3).toString());
            hijo.setApellidoMaterno(row.get(4).toString());
        }
        
        return hijo;
    }
    
    /**
     * Get all the cargos that exist in the database
     * @return {}
     * @throws SQLException
     * @throws Exception 
     */
    public static List<Hijos> getAll() throws SQLException, Exception {
        ArrayList<Hijos> hijos = new ArrayList<Hijos>();
        String[] columns = new String[] { ID, ID_OFICIAL, NOMBRE, APELLIDO_PATERNO, 
            APELLIDO_MATERNO };
        List<Object> rawCargo = caracolQuery.query(columns, TABLE, null, null, null, null, null);
        
        for (Object object : rawCargo) {
            List<Object> row = (List<Object>) object;
            Hijos hijo = new Hijos();
            hijo.id = new Double(row.get(0).toString()).intValue();
            hijo.setNombre(row.get(1).toString());
            hijo.setApellidoPaterno(row.get(2).toString());
            hijo.setApellidoMaterno(row.get(3).toString());
            
            hijos.add(hijo);
        }
        
        return hijos;
    }
    
    /** 
     * Saves the values of this instance in the database
     * @return true if the operation affects more than 0 rows
     * @throws SQLException if some exception related with the database happend during the excetion 
     * of this operation
     * @throws Exception if some exception happend during the operation
     */
    public boolean save() throws SQLException, Exception {
        if (getId() > 0) { // this is already save
            return true;
        }
        Map<String,Object> m = new LinkedHashMap<String,Object>();
        m.put(ID_OFICIAL, getIdOficial());
        m.put(NOMBRE, getNombre());
        m.put(APELLIDO_PATERNO, getApellidoPaterno());
        m.put(APELLIDO_MATERNO, getApellidoMaterno());
        
        int rowsAffected = caracolQuery.insert(TABLE, m);
        
        ArrayList<Object> data = null;
        if (rowsAffected > 0) {
            AccesoDatos ad = caracolQuery.getOpenConnection();
            data = new ArrayList<Object>(ad.rawQuery("SELECT currval(\'sqhijos\');"));
        }
        if (data != null && data.size() > 0) {
            ArrayList<Object> row = (ArrayList<Object>) data.get(0);
            Double temp = Double.parseDouble(row.get(0).toString());
            this.id = temp.intValue();
            return true;
        }
        return false;
    }

    /**
     * Updates the database row with this instance values
     * @return true if rows affected is > 0
     * @throws SQLException is some db exception happen this is throw
     * @throws Exception is some exception happen this is throw
     */
    public boolean update() throws SQLException, Exception {
        int rowsAffected;
        Map<String, Object> values = new LinkedHashMap<String, Object>();
        values.put(ID_OFICIAL, getIdOficial());
        values.put(NOMBRE, getNombre());
        values.put(APELLIDO_PATERNO, getApellidoPaterno());
        values.put(APELLIDO_MATERNO, getApellidoMaterno());
        
        Map<String,Object> conditions = new LinkedHashMap<String, Object>();
        conditions.put(ID, getId());
        
        rowsAffected = caracolQuery.update(TABLE, values, conditions);
        
        return rowsAffected > 0;
    }
    
    /** 
     * Delete the cargo from database this delete is made by searching in the database if this instance id exist
     * 
     * @return true if the operation affect more thant one row otherwise returns false
     * @throws SQLException if the database throws an exception during the execution of this operation
     * @throws Exception if something other than the database throws an exception this is throw
     * 
     */
    public boolean delete() throws SQLException, Exception {
        if (getId() <= 0) {
            return true;
        }

        Map<String, Object> conditions = new LinkedHashMap<String, Object>();
        conditions.put(ID, getId());
        int rowsAffected = caracolQuery.delete(TABLE, conditions);

        if (rowsAffected > 0) {
            id = 0;
        }

        return rowsAffected > 0;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @return the idOficial
     */
    public int getIdOficial() {
        return idOficial;
    }

    /**
     * @param idOficial the idOficial to set
     */
    public void setIdOficial(int idOficial) {
        this.idOficial = idOficial;
    }

    /**
     * @return the nombre
     */
    public String getNombre() {
        return nombre;
    }

    /**
     * @param nombre the nombre to set
     */
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    /**
     * @return the apellidoPaterno
     */
    public String getApellidoPaterno() {
        return apellidoPaterno;
    }

    /**
     * @param apellidoPaterno the apellidoPaterno to set
     */
    public void setApellidoPaterno(String apellidoPaterno) {
        this.apellidoPaterno = apellidoPaterno;
    }

    /**
     * @return the apellidoMaterno
     */
    public String getApellidoMaterno() {
        return apellidoMaterno;
    }

    /**
     * @param apellidoMaterno the apellidoMaterno to set
     */
    public void setApellidoMaterno(String apellidoMaterno) {
        this.apellidoMaterno = apellidoMaterno;
    }
    
}
